package com.taojiazheng.serviceImpl.favorite;

import com.taojiazheng.common.ApplicationException;
import com.taojiazheng.dao.favorite.FavoriteDao;
import com.taojiazheng.dao.user.UserDao;
import com.taojiazheng.entity.Favoritejob;
import com.taojiazheng.entity.Favoriteuser;
import com.taojiazheng.entity.User;
import com.taojiazheng.service.favorite.FavoriteService;
import com.taojiazheng.vo.favorite.FavoriteJobVo;
import com.taojiazheng.vo.favorite.FavoriteUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵成林
 * @date 2017/12/23 22:34
 */
@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Autowired
    private FavoriteDao favoriteDao;

    @Autowired
    private UserDao userDao;

    public int createFavoriteJob(Favoritejob favoritejob) {
        //return 0;
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("userId",favoritejob.getUserId());
        map.put("jobId",favoritejob.getJobId());
//        System.out.println(favoritejob.getUserId());
//        System.out.println(favoritejob.getJobId());
        boolean value = this.isCreateFavoriteJob(map);
        if(!value){
            throw new ApplicationException(211,"已经收藏过该岗位");
        }
        return favoriteDao.createFavotiteJob(favoritejob);
    }

    public int createFavoriteUser(Favoriteuser favoriteuser) {
        //return 0;
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("userId",favoriteuser.getUserId());
        map.put("favoriteUserId",favoriteuser.getFavoriteUserId());
        boolean value = this.isCreateFavoriteUser(map);
        if(!value){
            throw new ApplicationException(212,"已经收藏过此人");
        }
        return favoriteDao.createFavoriteUser(favoriteuser);
    }

    /**
     * 判断是否可以收藏岗位
     * 不能重复收藏
     * @param map
     * @return
     */
    private boolean isCreateFavoriteJob(Map map){
        List<Favoritejob> favoritejobs = favoriteDao.getFavoriteJob(map);
        //System.out.println(favoritejobs == null);
        if(favoritejobs != null && favoritejobs.size()!=0){
            return false;
        }
        return true;
    }

    private boolean isCreateFavoriteUser(Map map){
        List<Favoriteuser> favoriteusers = favoriteDao.getFavoriteUser(map);
        if(favoriteusers != null && favoriteusers.size()!= 0){
            return false;
        }
        return true;
    }

    public List<FavoriteUserVo> getUserList(String token) {
        //return null;
        User user = userDao.getUserByToken(token);
        return favoriteDao.getUserList(user.getId());
    }

    public List<FavoriteJobVo> getJobList(String token) {
        //return null;
        User user = userDao.getUserByToken(token);
        return favoriteDao.getJobList(user.getId());
    }
    /**
     * 删除收藏的工作岗位
     */
    public void deleteFavoriteJob(Integer id) {
    	favoriteDao.deleteFavoriteJob(id);
    }
    
    public void deleteFavoriteUser(Integer id) {
    	favoriteDao.deleteFavoriteUser(id);
    }
}
